### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata_drop_Winter <- read.dta("Analysis Data/NIBRS_analysis_drop_winter.dta")
mydata_drop_Spring <- read.dta("Analysis Data/NIBRS_analysis_drop_spring.dta")
mydata_drop_Summer <- read.dta("Analysis Data/NIBRS_analysis_drop_summer.dta")  
mydata_drop_Fall <- read.dta("Analysis Data/NIBRS_analysis_drop_fall.dta")  

# Panel A: DiT Approach

DiT_3_DWinter <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata_drop_Winter)
summary(DiT_3_DWinter, cluster="STATE")
DiT_3_DSpring <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata_drop_Spring)
summary(DiT_3_DSpring, cluster="STATE")
DiT_3_DSummer <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata_drop_Summer)
summary(DiT_3_DSummer, cluster="STATE")
DiT_3_DFall <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata_drop_Fall)
summary(DiT_3_DFall, cluster="STATE")

models <- list(DiT_3_DWinter, DiT_3_DSpring, DiT_3_DSummer, DiT_3_DFall)
modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_B3/Table_B3_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel B: DiDiT Approach

DiDiT_6_DWinter <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata_drop_Winter)
summary(DiDiT_6_DWinter, cluster="STATE")
DiDiT_6_DSpring <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata_drop_Spring)
summary(DiDiT_6_DSpring, cluster="STATE")
DiDiT_6_DSummer <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata_drop_Summer)
summary(DiDiT_6_DSummer, cluster="STATE")
DiDiT_6_DFall <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata_drop_Fall)
summary(DiDiT_6_DFall, cluster="STATE")

models <- list(DiDiT_6_DWinter, DiDiT_6_DSpring, DiDiT_6_DSummer, DiDiT_6_DFall)
modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_B3/Table_B3_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

